package com.tedu.egweibo1.controller;

import com.tedu.egweibo1.mapper.CommentMapper;
import com.tedu.egweibo1.pojo.dto.CommentDTO;

import com.tedu.egweibo1.pojo.entity.Comment;
import com.tedu.egweibo1.pojo.entity.User;
import com.tedu.egweibo1.result.JsonResult;
import com.tedu.egweibo1.result.StatusCode;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpSession;
import java.util.Date;


@Api(tags = "评论模块")
@RestController
@RequestMapping("/v1/comment")
public class CommentController {
    @Autowired
    private CommentMapper commentMapper;


    @PostMapping("/insert")
    @ApiOperation("发布评论功能")
    public JsonResult insert(@RequestBody CommentDTO commentDTO, @ApiIgnore HttpSession session) {
        User user = (User) session.getAttribute("user");
        if (user!=null){
            Comment comment = new Comment();
            BeanUtils.copyProperties(commentDTO,comment);
            comment.setUserId(user.getId());
            comment.setCreated(new Date());
            commentMapper.InsertComment(comment);
            return JsonResult.success();
        }
        return new JsonResult(StatusCode.NOT_LOGIN);



    }
}
